BLOCK.Model = "models/props_combine/combine_mine01.mdl";
BLOCK.BlockName = "Mine";
BLOCK.Desc = "Invisible touch-mine.";
BLOCK.Health = 1000;
BLOCK.MaxMines = 5;

function BLOCK:Init()
	self.SENT.Entity:DrawShadow(false);
end

function BLOCK:Touch( ent )
	//If they are not on our team
	if(ent:IsPlayer() && ent:Team() != self.Team) then
		//Get our creator
		local ply = player.GetByID(self.SENT.userid);
		
		//If our creator is still valid (in-game)
		if(ply != NULL && ply != nil) then
			//If the player is still on our team
			if(ply:Team() == self.Team) then 
				//Explode
				self:Blast(ply);
				return;
			end
		end
		//If the player left or switched teams, find another player on our team
		local players = player.GetAll();
		for key, val in pairs(players) do
			if(val:Team() == self.Team) then
				//When found give them kills
				self:Blast(ply);
				return;
			end
		end
		//If no players can be found, then just attribute deaths to us
		self:Blast(self.SENT);
	end
end

function BLOCK:ValidPos(vec, err, ply)
	if(err) then
		local blocks = ents.FindByClass("bh_block");
		local builders = ents.FindByClass("bh_builder");
		table.Add(blocks, builders);
		local mine_counter = 0;
		for key, val in pairs(blocks) do
			if(val.blockName == "mine" && ply:Team() == val:GetTeam()) then 
				mine_counter = mine_counter + 1; 
			end
		end
		if(mine_counter >= self.MaxMines) then
			hook.Call("ShowNotice", nil, "Too many mines!", ply);
			return false;
		end
	end
	return true;
end

function BLOCK:Blast( ply )
	local damage = 100;
	local radius = 300;
	local p = self.SENT:GetPos();
	
	//Create a blast damage and explosion effect
	util.BlastDamage(ply, ply, p, radius, damage);
	local e = EffectData();
	e:SetStart(p);
	e:SetOrigin(p);
	e:SetScale(1);
	util.Effect("Explosion", e);
	self.SENT:Remove();
end

function BLOCK:Render()
	if(!CLIENT) then return; end
	//If they are on our team, draw the model, else do not
	if(LocalPlayer():Team() == self.Team) then self.SENT:DrawModel();
	else return; end
end